import numpy as np
import math as m

class ARAS:
    def __init__(self,NormalizeMatrix, Weight,q):
        '''
        :param NormalizeMatrix: 矩阵
        :param Weight: 权重
        :param Gangliang: 属性的成本或效益型
        '''
        self.Matrix = np.array(NormalizeMatrix)
        self.Weight = Weight
        self.lines = len(NormalizeMatrix)  # 行数
        self.colums = len(NormalizeMatrix[0])  # 列数
        self.Gangliang = [0,0]+[1 for i in range(self.colums-2)]
        self.q = q

    def ARAS(self):
        Arr = self.Matrix
        # print(np.shape(Arr))
        ##加权归一化
        q = self.q
        Weights=self.Weight
        temp = np.zeros((len(Arr),len(Arr[0]), len(Arr[0][0]), len(Arr[0][0][0])))
        for Row in range(len(Arr)):
            for Column in range(len(Arr[0])):
                w = Weights[Column]
                ua1 = Arr[Row][Column][0][0]
                ua2 = Arr[Row][Column][0][1]
                va1 = Arr[Row][Column][1][0]
                va2 = Arr[Row][Column][1][1]
                ua1 = (1 - (1 - ua1 ** q) ** w) ** (1 / q)
                ua2 = (1 - (1 - ua2 ** q) ** w) ** (1 / q)
                va1 = va1 ** w
                va2 = va2 ** w
                temp[Row][Column][0][0] = ua1
                temp[Row][Column][0][1] = ua2
                temp[Row][Column][1][0] = va1
                temp[Row][Column][1][1] = va2
        # return temp

        ##最优性函数
        Arr = temp
        tempt = np.zeros((len(Arr),1, len(Arr[0][0]), len(Arr[0][0][0])))
        for Row in range(len(Arr)):
            sua1,sua2,sva1,sva2=0,0,1,1
            for Column in range(len(Arr[0])):
                ua1 = Arr[Row][Column][0][0]
                ua2 = Arr[Row][Column][0][1]
                va1 = Arr[Row][Column][1][0]
                va2 = Arr[Row][Column][1][1]
                sua1 =(ua1**q+sua1**q-ua1**q*sua1**q)**(1/q)
                sua2 =(ua2**q+sua2**q-ua2**q*sua2**q)**(1/q)
                sva1 = va1 ** sva1
                sva2 = va2 ** sva2
            tempt[Row][0][0][0] = sua1
            tempt[Row][0][0][1] = sua2
            tempt[Row][0][1][0] = sva1
            tempt[Row][0][1][1] = sva2
        return tempt

    def ScoreFunction(self):
        Arr = self.ARAS()
        aa=[]
        for Row in range(len(Arr)):
            a = Arr[Row][0][0][0]
            b = Arr[Row][0][0][1]
            c = Arr[Row][0][1][0]
            d = Arr[Row][0][1][1]
            score = (1 / (2 * m.log(3))) * (m.log(a + b + c + d + 1) + m.log(((b - a) + (d - c) ** 2 + 2) / 2) + (a - c + b - d) * m.log(3) / 2)
            aa.append(score)
        return aa

    def getResult(self):
        result = self.ScoreFunction()
        result=[i/sum(result) for i in result]
        return result


if __name__=="__main__":
    data = np.array([[([0.58, 0.75], [0.1, 0.2]), ([0.7, 0.78], [0.2, 0.3]), ([0.5, 0.78], [0.1, 0.2]),
               ([0.65, 0.7], [0.3, 0.35]), ([0.4, 0.5], [0.5, 0.6])],
              [([0.5, 0.6], [0.1, 0.2]), ([0.75, 0.78], [0.2, 0.3]), ([0.6, 0.78], [0.1, 0.15]),
               ([0.7, 0.8], [0.2, 0.3]), ([0.5, 0.6], [0.4, 0.5])],
              [([0.5, 0.78], [0.1, 0.2]), ([0.6, 0.7], [0.3, 0.4]), ([0.7, 0.78], [0.2, 0.3]), ([0.6, 0.7], [0.3, 0.4]),
               ([0.35, 0.45], [0.5, 0.65])],
              [([0.75, 0.780], [0.2, 0.3]), ([0.6, 0.75], [0.2, 0.3]), ([0.7, 0.78], [0.2, 0.3]),
               ([0.55, 0.65], [0.3, 0.4]), ([0.35, 0.4], [0.6, 0.65])],
              [([0.6, 0.75], [0.3, 0.4]), ([0.5, 0.6], [0.4, 0.5]), ([0.6, 0.7], [0.3, 0.4]), ([0.5, 0.6], [0.4, 0.5]),
               ([0.1, 0.2], [0.4, 0.5])]])
    weight = [0.171, 0.185, 0.177, 0.225, 0.157]
    gangliang = [0, 0, 1, 0, 0]
    q=3
    
    AR = ARAS(data,weight,q)
    result=AR.getResult()
    print(result)






